#stargazer <= 5.2.3 has a known issue in R >= 4.2. If stargazer calls in the main R script throw errors, run the following code to implement a workaround for this issue.

#adapted from code by Alex Knorre

#unload stargazer if loaded

detach("package:stargazer", unload = TRUE)

#remove stargazer package

remove.packages("stargazer")

#download stargazer source code

download.file("https://cran.r-project.org/src/contrib/stargazer_5.2.3.tar.gz",
              destfile = "stargazer_5.2.3.tar.gz")

#unpack source code

untar("stargazer_5.2.3.tar.gz")

#read source code

stargazer_src <- readLines("stargazer/R/stargazer-internal.R")

#move length check to precede is.na(.)

stargazer_src[1990] <- stargazer_src[1995]
stargazer_src[1995] <- ""

#save function back to source file

writeLines(stargazer_src, con = "stargazer/R/stargazer-internal.R")

#compile and install stargazer with patch

install.packages("stargazer", repos = NULL, type = "source")
library(stargazer)
